package com.itheima.service;

import com.itheima.dao.BaseUserDao;
import com.itheima.dao.impl.UserDao;
import com.itheima.domain.Option;
import com.itheima.domain.Pow;
import com.itheima.domain.User;
import com.itheima.factory.UserDaoFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.List;

public class UserService implements Runnable {
    private static Socket socket;
    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
    BaseUserDao dao = UserDaoFactory.getUserDao();

    public UserService(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //接收用户端传过来的报文数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String allMsg = br.readLine();
            LOGGER.info("收到来自客户端" + socket.getLocalAddress() + "的报文数据: " + allMsg);
            //解析报文数据
            String[] split = allMsg.split("&");
            Option option = Enum.valueOf(Option.class,split[0]);
            String optionMsg = split[1];
            switch (option){
                case ADD_USER:
                    addUser(optionMsg);
                    break;
                case DELECT_USER:
                    delectUser(optionMsg);
                    break;
                case UPDATE_USER:
                    updateUser(optionMsg);
                    break;
                case FINDALL_USER:
                    findAllUser();
                    break;
                case LOGIN:
                    login(optionMsg);
                    break;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void login(String optionMsg) {
        LOGGER.info("执行查询操作:");
        //1. 调用dao,拿到系统数据
        User user = dao.getUser(optionMsg);

        try {
            //2. 通过socket将对象写给客户端
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);
            oos.close();
            socket.close();
            LOGGER.info("查看完成,响应客户端数据:" + user);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("查找中,服务器异常:" + e.getMessage());
        }
    }

    private void findAllUser() {
        LOGGER.info("执行查询操作:");
        //1. 调用dao,拿到系统数据
        List<User> list = dao.findAllUser();

        try {
            //2. 通过socket将整个集合对象写给客户端
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(list);
            oos.close();
            socket.close();
            LOGGER.info("查看完成,响应客户端数据:" + list);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("查找中,服务器异常:" + e.getMessage());
        }
    }

    private void delectUser(String optionMsg) {
        LOGGER.info("删除学生信息,数据:" + optionMsg);
        //解析业务数据
        String[] split = optionMsg.split(",");
        //1.调用dao层删除操作
        boolean res = dao.deleteUser(split[0],Enum.valueOf(Pow.class,split[1]));
        //响应结果给客户端
        String resultInfo = res ? "删除成功" : "权限不足或用户不存在,无法删除";

        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.newLine();
            bw.flush();

            bw.close();
            socket.close();
            LOGGER.info("删除完成,响应客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("删除中,服务器异常:" + e.getMessage());
        }
    }

    private void updateUser(String optionMsg) {
        LOGGER.info("执行修改操作,接收数据:" + optionMsg);
        //解析业务数据
        String[] split = optionMsg.split(",");
        User user = new User(split[0],split[1],split[2]);
        //调用dao层方法,实现修改
        boolean res = dao.updateUser(split[0],user);

        //响应结果回客户端
        String resultInfo = res == true ? "修改成功!": "该用户名不存在,修改失败!";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.newLine();
            bw.flush();

            bw.close();
            socket.close();
            LOGGER.info("修改完成,响应客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("修改时,服务器异常:" + e.getMessage());
        }
    }

    private void addUser(String optionMsg) {
        LOGGER.info("执行添加操作,接收数据:" + optionMsg);
        //解析业务数据
        String[] split = optionMsg.split(",");
        User user = new User(split[0],split[1],split[2]);
        //调用dao层方法,实现添加
        boolean res = dao.addUser(user);

        //响应结果回客户端
        String resultInfo = res == true ? "添加成功!": "该用户名已存在,添加失败!";
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(resultInfo);
            bw.newLine();
            bw.flush();

            bw.close();
            socket.close();
            LOGGER.info("添加完成,响应客户端数据:" + resultInfo);
        } catch (IOException e) {
            //e.printStackTrace();
            LOGGER.error("添加中,服务器异常:" + e.getMessage());
        }

    }

}
